package leetcode_cn;

/**
 * 最长公共前缀
 *
 * @author briarbear
 * @blog https://briarbear.github.io
 * @date 2018-09-06 10:22
 */
public class LongestCommonPrefix {

    public String solution(String[] strs){
        int len = strs.length;  //字符串数组的长度
        StringBuilder sb = new StringBuilder();
        if (strs == null || strs.length==0) return sb.toString();
        int index = 0;
        while (true){
            for (int i = 0; i < len; i++) {
                int tLen = strs[i].length();
                if (index >= tLen) return sb.toString();
                if (strs[0].charAt(index) != strs[i].charAt(index)) return sb.toString();
            }
                sb.append(strs[0].charAt(index));
            index++;
        }

    }

    public static void main(String[] args) {
        String[] strs = new String[]{"dog","racecar","car"};
        System.out.println(new LongestCommonPrefix().solution(strs));
    }
}
